package vista;

import daoBeans.conexionMySql;
import daoBeans.daoUsuario;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ApplicationScoped;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;
import modelo.Usuario;

@ManagedBean
@SessionScoped
public class loginBean {

    private String username;
    private String password;
    private conexionMySql sql = new conexionMySql();
    private Connection mysql;
    private static Usuario other = new Usuario();
    private daoUsuario daou;
    

    public loginBean() {
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public void init() {
        mysql = sql.comenzarDb();
        daou = new daoUsuario(mysql);
    }

    public Usuario getOther() {
        return other;
    }

    public void setOther(Usuario other) {
        this.other = other;
    }

    public String login() {

        init();

        String login = "invalido";

        if (!verificarLongitudNombre(getUsername())) {
            FacesContext context = FacesContext.getCurrentInstance();
            context.addMessage("username", new FacesMessage("Longitud de nombre incorrecta"));
        }
        if (!verificarLongitudPassword(getPassword())) {
            FacesContext context = FacesContext.getCurrentInstance();
            context.addMessage("username", new FacesMessage("longitud de contraseña incorrecta"));
        }


        other = daou.buscarUsuarioUsername(getUsername());

        if (other != null && other.getUsername().equals(username) && other.getPassword().equals(password)) {
            try {
                mysql.close();
            } catch (SQLException ex) {
                Logger.getLogger(loginBean.class.getName()).log(Level.SEVERE, null, ex);
            }

            login = other.getRol();

        } else {
            FacesContext context = FacesContext.getCurrentInstance();
            context.addMessage("username", new FacesMessage(FacesMessage.SEVERITY_WARN, "Error de ingreso", "Datos incorrectos"));
        }

        return login;
    }

    public boolean verificarLongitudNombre(String nombre) {
        return (nombre.length() > 1 && nombre.length() <= 12);
    }

    public boolean verificarLongitudPassword(String password) {
        return (password.length() >= 3 && password.length() < 12);
    }

    public String logout() {
        FacesContext.getCurrentInstance().getExternalContext().invalidateSession();
        return "index?faces-redirect=true";
    }

    public boolean isLoggedIn() {
            return other != null;
    }
}
